<?php
/**
 * 菜单规则模型
 */
namespace app\common\model;

// 引入框架内置类
use think\facade\Request;


class MembershipRecords extends Base
{
	// 开启自动写入时间戳字段
	protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $name = 'membership_records';

    public function card(){
        return $this->hasMany(MembershipCard::class,"record_id");
    }
    public function chargeOrder(){
        return $this->hasMany(MembershipInterests::class,"record_id");
    }
    public function packageCard(){
        return $this->hasMany(PackageCard::class,"record_id");
    }
    // 获取父ID选项信息
    public static function getList($where=null)//$order = ['weigh', 'id' => 'desc']
    {
        //$where1 = MembershipCard::where('record_id',861)->where('card_id',1);
        //$list = self::with(['card'=>['interests'=>function($query){$query->where('is_pay',1);},'packageCard']])
        // -> with(['card'=>function($query){$query->where('type',1);}])
        $a = ['card'=>['interests','packageCard','summarys']]; 
        $b = ['card'=>function($query){$query->where('type',3);}];
      
        $c = ['card'=>['interests','packageCard'],'summarys'];
        $list = self::with($c)
        ->hasWhere('summarys',function($query1){
            //$query1->order('count','desc');
        })
        //->hasWhere('card', ['type'=>1])
        //->with(['summarys'=>function($query){$query->order('count desc');}])
        
        
        /*->hasWhere('summarys',function($query1){
            $query1->where('count','like','2%');
        })*/
        
       //->hasWhere('summarys',['order','count'=>'desc'])
       
        ->where($where)
        ->fetchSql(false)
        ->order('count','desc')
        //->limit(0,2)
        //->select()
        //->order('count','desc')
        /*->paginate([
        'list_rows'=>2,
        'page'=>1
        ])*/
        ->limit(0,2)
        ->select()
        ->toArray();

        //->order('count','desc')
        //->toArray();

        
        //$list= $list->hidden(['card.type'])->toArray();
        return $list;
    }
    public function summarys(){
        
        return  $this->hasOne(Summary::class,"card_id");//->bind(['count','left_amount','cost_amount','amount']);
        
        
    }
    public function summary(){
       return  $this->hasOne(Summary::class,"card_id"); 
    }
    /**
     * 获取会员卡类型。
     */
   /* public function comments(){
        return $this->morphMany(MembershipCard::class, 'type',1);

        
    }
    public static function test(){

        $list = self::with('comments')->select()->toArray();
        return $list;

    }*/
   
}
